home *** CD-ROM | disk | FTP | other *** search
/ Aminet 40 / Aminet 40 (2000)(Schatztruhe)[!][Dec 2000].iso / Aminet / dev / c / ExtrasLib.lha / ExtrasLib / Source / GetEntryData.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-09-30  |  4.2 KB  |  191 lines

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. #include <clib/extras_protos.h>
  6. #include <proto/dos.h>
  7. #include <proto/exec.h>
  8.  
  9. #include <extras/entry.h>
  10.  
  11. #include <exec/types.h>
  12. #include <exec/memory.h>
  13.  
  14. #include <extras/macros.h>
  15. #include "entries.h"
  16.  
  17.  
  18.  
  19. /****** extras.lib/db_GetEntryData ******************************************
  20. *
  21. *   NAME
  22. *       db_GetEntryData -- Retrieve data from an ENTRY of a database.
  23. *
  24. *   SYNOPSIS
  25. *       db_GetEntryDataA(File, EntryName, Items) -- NOT implemented --
  26. *
  27. *       BOOL db_GetEntryDataA(BPTR, STRPTR struct EItem *);
  28. *
  29. *       db_GetEntryData(File, EntryName, Items, ... )
  30. *
  31. *       BOOL db_GetEntryData(BPTR, STRPTR, STRPTR, ... );
  32. *
  33. *   FUNCTION
  34. *       Retrieve data from a simple ENTRY based database.
  35. *
  36. *   INPUTS
  37. *       File - an AmigaDOS BPTR to a file.
  38. *       Name - an array of struct Etems, the last struct EItem should have
  39. *              it's Name field set to NULL. (see example)
  40. *   RESULT
  41. *       returns 0 on failure, possibly due to EOF, improper file format, or
  42. *       lack of memory.  
  43. *    
  44. *       To support multiple occurances of an item Name in an Entry, this 
  45. *       function returns NNStrings.  The strings are stored end to end in 
  46. *       the order that they were read from the file.
  47. *
  48. *       On success, each EItem.ReturnString either points to an NNString,
  49. *       or NULL if no data for that Name was found.
  50. *
  51. *       On failure, all EItem.ReturnStrings are NULL, and any data collected
  52. *       is freed.
  53. *
  54. *   EXAMPLE
  55. *       STRPTR title,desc;
  56. *       BPTR File;
  57. *
  58. *       if(db_GetEntryData(File,"ENTRY",
  59. *                            "TITLE"  ,&title,
  60. *                            "DESC"   ,&desc,
  61. *                               0))
  62. *       {
  63. *         if(title)
  64. *         {
  65. *           printf("%s - ",title);
  66. #           FreeVec(title);
  67. *         }
  68. *
  69. *         if(desc)
  70. *         {
  71. *           printf("%s\n");
  72. *           FreeVec(desc);
  73. *         }
  74. *       }
  75. *
  76. *
  77. *   NOTES
  78. *       The database file format is an ASCII text file, and consists
  79. *       of "ENTRY"'s, that look like this:
  80. *
  81. *       <ENTRYNAME>
  82. *       {
  83. *         <ITEMNAME> = <data>
  84. *         <ITEMNAME> = <data>
  85. *       }
  86. *
  87. *       an example file format from above might be:
  88. *
  89. *       ENTRY
  90. *       {
  91. *         TITLE=Cows 'R Us
  92. *         DESC=All you want to know about beef.
  93. *       }
  94. *
  95. *       case of <ENTRYNAME> and <ITEMNAMES> is not important.
  96. *       the equal sign is required.
  97. *       
  98. *   HISTORY
  99. *       This code probably isn't all that usefull, but it was the
  100. *       code behind the database of now defunct Tampa Bay Amiga Group's
  101. *       Amiga Support Directory.  Unfortunately, TBAG died before the
  102. *       ASD could begine to grow, and I haven't used this code since.
  103. *
  104. *   BUGS
  105. *       Not reentrant.
  106. *
  107. *   SEE ALSO
  108. *       nns_ProcessNNStr, nns_AddNNStr(), nns_NextNNStr(), db_EntryToNN()
  109. *
  110. ******************************************************************************
  111. *
  112. */
  113.  
  114. BOOL db_GetEntryData(BPTR File, STRPTR EntryName, STRPTR Name, ... )
  115. {
  116.   struct nitem
  117.   {
  118.     STRPTR  Name,
  119.           *Value;
  120.   } *ni;
  121.   
  122.   if(!GED_Buffer)
  123.   {
  124.     GED_Buffer=malloc(BUFFERSIZE);
  125.     if(!GED_Buffer)
  126.       return(0);
  127.   }
  128.   
  129.   GED_Buffer[0]=0;
  130.  
  131.   ni=(struct nitem *)&Name;
  132.   while(ni->Name)
  133.   {
  134.     *ni->Value=0;
  135.     ni++;
  136.   }
  137.  
  138.   
  139.   if(db_NextEntry(File,EntryName,GED_Buffer,BUFFERSIZE))
  140.   {
  141.     GED_Buffer[0]=0;
  142.     while(GED_Buffer[0]!='}')
  143.     {
  144.       if(!FGets(File,GED_Buffer,BUFFERSIZE))
  145.         return(0);
  146.       
  147.       Strip(GED_Buffer);
  148.   
  149.       ni=(struct nitem *)&Name;
  150.       while(ni->Name)
  151.       {
  152.         LONG namelen;
  153.         
  154.         namelen=strlen(ni->Name);
  155.         
  156.         if(strnicmp(GED_Buffer,ni->Name,namelen)==0)
  157.         {
  158.           STRPTR value;
  159.           
  160.           value=GED_Buffer+namelen;
  161.           
  162.           while(IsWhiteSpace(*value))
  163.           {
  164.             value++;
  165.           }
  166.         
  167.           if(*value=='=')
  168.           {
  169.             value++;
  170.             Strip(value);
  171.             if(!(*ni->Value=nns_AddNNStr(*ni->Value,value)))
  172.             {
  173.               while(ni->Name)
  174.               {
  175.                 FreeVec(*ni->Value);
  176.                 *ni->Value=0;
  177.                 ni++;
  178.               }
  179.               return(0);
  180.             }
  181.           }
  182.         }
  183.         ni++;
  184.       }
  185.     }
  186.     return(1);
  187.   }
  188.   return(0);
  189. }
  190.  
  191.